home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / editor / snmp_0_1.zip / snmp-0.1 / aka / snmp / PDU.java < prev    next >
Text File  |  1997-06-09  |  8KB  |  322 lines

  1. /*
  2. Snmp Library
  3. Copyright (C) 1997 Alex Kowalenko Associates Pty Ltd. All rights reserved.
  4.  
  5. This software maybe be free distributed, any any form, without fee, 
  6. but may not be modified in any way without express permission of 
  7. the directors of Alex Kowalenko Associates Pty Ltd. 
  8.  
  9. Alex Kowalenko Associates Pty Ltd makes no representations or
  10. warranties about the suitabililty of the software, not even the
  11. implied warranty of merchantability or fitness for any particular
  12. purpose.    
  13. */
  14.  
  15. package aka.snmp;
  16.  
  17. import java.util.*;
  18.  
  19. /**
  20.  * This class implements a SNMP Protocol Data Unit (PDU).  These are 
  21.  * the packets of information that are sent back and forth between 
  22.  * SNMP agents and clients.
  23.  * This class models these packets and will convert to and from,
  24.  * sequences of characters according to the rules of the SNMP
  25.  * protocol and BER encoding.
  26.  * @see PDUVar
  27.  * @version     $Id: PDU.java,v 1.4 1997/05/18 08:11:53 alex Exp $
  28.  * @author      Alex Kowalenko
  29.  */
  30.  
  31. public class PDU implements BERSerializable {
  32.   
  33.   private static int thisVersion = 0; // SNMP Version 1 Protocol
  34.  
  35.   private boolean _valid;
  36.   private int _version;
  37.   private String _address;
  38.   private Operation _command;
  39.   private String _communityName;
  40.   private int _requestId;
  41.   private SnmpError _errorStatus;
  42.   private int _errorIndex;
  43.   private Vector _varList; // Vector<PDUVariable>
  44.  
  45. /**
  46.  * Construct a PDU packet with the address addr, SNMP operation
  47.  * command, Community Name cn, and Request Identification
  48.  * number redId.
  49.  */
  50.  
  51.     PDU(String addr, byte command, String cn, int reqId) {
  52.     _valid = true;
  53.     _version = thisVersion;
  54.     _address = addr;
  55.     _command = new Operation(command);
  56.     _communityName = cn;
  57.     _requestId = reqId;
  58.     _errorStatus = new SnmpError(SnmpError.NO_ERROR);
  59.     _errorIndex = 0;
  60.     _varList = new Vector();
  61.     };
  62.   
  63. /** 
  64.  * Construct a PDU packet from the sequence of characters in buffer. 
  65.  */
  66.  
  67.     PDU(ByteBuffer buffer) throws SnmpPDUException {
  68.     _valid = false;
  69.     if(buffer.size() == 0)
  70.         return;
  71.     
  72.     // SEQUENCE | CONSTRUCTOR
  73.     if(!(buffer.byteAt(0) == (byte) (ASN.SEQUENCE|ASN.CONSTRUCTOR))) {
  74.         throw new SnmpPDUException("Packet is not PDU");
  75.     };
  76.     buffer.removeBeginning(1);
  77.  
  78.     // Get length
  79.     int length = ASN.getLength(buffer);// Total length of the packet.
  80.     if(length >  buffer.size()) {
  81.         throw new SnmpPDUException("Packet length " + length + 
  82.                        " doesn't match as specified " 
  83. + buffer.size());
  84.     }
  85.     else if(length < buffer.size())
  86.         buffer.truncate((int)length);
  87.  
  88.     // Parse Version number
  89.     Type version = Type.parse(buffer);
  90.     if(!version.typeName().equals(TypeInt.name)) {
  91.         throw new SnmpPDUException("Snmp version type wrong.");
  92.     }
  93.     _version = ((TypeInt)version).value();
  94.     
  95.     // Parse Community Name
  96.     Type communityName = Type.parse(buffer);
  97.     if(!communityName.typeName().equals(TypeString.name)) {
  98.         throw new SnmpPDUException("Snmp Community Name not found");
  99.     }
  100.     _communityName = ((TypeString)communityName).value();
  101.  
  102.     // Parse Snmp Operation
  103.     byte snmpAction = buffer.byteAt(0);
  104.     _command = new Operation((byte) (snmpAction & ~(ASN.CONTEXT) & ~(ASN.CONSTRUCTOR)));
  105.     buffer.removeBeginning(1);
  106.     length = ASN.getLength(buffer);
  107.     
  108.     // Parse Request id
  109.     Type reqID = Type.parse(buffer);
  110.     if(!reqID.typeName().equals(TypeInt.name)) {
  111.         throw new SnmpPDUException("Expecting Request type Integer");
  112.     };
  113.     _requestId = (int)((TypeInt)reqID).value();
  114.  
  115.     // Parse error-status
  116.     Type errStat = Type.parse(buffer);
  117.     if(!errStat.typeName().equals(TypeInt.name)) {
  118.         throw new SnmpPDUException("Expecting Error Status");
  119.     }
  120.     _errorStatus = new SnmpError(((TypeInt)errStat).value());
  121.     
  122.     // Parse error index
  123.     Type errIdx = Type.parse(buffer);
  124.     if(!errIdx.typeName().equals(TypeInt.name)) {
  125.         throw new SnmpPDUException("Expecting Error Index");
  126.     };
  127.     _errorIndex = (int) ((TypeInt)errIdx).value();
  128.     
  129.     // Parse Pdu Variables
  130.     if(!(buffer.byteAt(0) == (byte) (ASN.SEQUENCE | ASN.CONSTRUCTOR))) {
  131.         throw new SnmpPDUException("Expecting variable-values");
  132.     };
  133.     buffer.removeBeginning(1);
  134.  
  135.     // Get length
  136.     int subLength = ASN.getLength(buffer);
  137.     
  138.     _varList = new Vector(2);
  139.     while(buffer.size() != 0)
  140.     {
  141.         PDUVariable pduVar = new PDUVariable(buffer);
  142.         _varList.addElement(pduVar);
  143.     }
  144.     _valid = true;
  145.     };
  146.  
  147. /**
  148.  *  Construct an empty PDU packet
  149.  */
  150.  
  151.     PDU() {
  152.     _valid = false;
  153.     };
  154.   
  155. /**
  156.  * Add this variable <oid> with this value <value> to the
  157.  * list of variables.  Used for SNMP Set operations.
  158.  */
  159.  
  160.     void addVar(ObjectId oid, Type value) {
  161.     PDUVariable var = new PDUVariable(oid, value);
  162.     _varList.addElement(var);
  163.     };
  164.    
  165. /**
  166.  * Add this variable <oid> to the list of variables.  Used for
  167.  * SNMP Get and GetNext operations.
  168.  */
  169.  
  170.     void addVar(ObjectId oid) {
  171.     Type t = new TypeNull();
  172.     PDUVariable pv = new PDUVariable(oid, t);
  173.     _varList.addElement(pv);
  174.     }
  175.  
  176. /**
  177.  * set Adress of PDU
  178.  */
  179.  
  180.     void setAddress(String addr) {
  181.     _address = addr;
  182.     }
  183.  
  184. /**
  185.  * Is this PDU valid, was a sequence of characters decoded into
  186.  * a valid PDU.
  187.  */
  188.  
  189.     boolean isValid() {
  190.     return _valid;
  191.     }
  192.  
  193. /**
  194.  * Return the request identifier number of this request.
  195.  */
  196.     int requestID() {
  197.     return _requestId;
  198.     }
  199.  
  200. /**
  201.  * What is the error status of this PDU.
  202.  */
  203.  
  204.     SnmpError errorStatus() {
  205.     return _errorStatus;
  206.     };
  207.  
  208. /**
  209.  * The error index points to which varaible.
  210.  */
  211.  
  212.     int errorIndex() {
  213.     return _errorIndex;
  214.     };
  215.   
  216. /**
  217.  * The number of variable-value pairs in this PDU.
  218.  */
  219.  
  220.     int numOfVariables() {
  221.     return _varList.size();
  222.     };
  223.  
  224. /**
  225.  * Return the Variable-value pair pointed to by the index.
  226.  */
  227.     PDUVariable var(int index) {
  228.     return (PDUVariable) _varList.elementAt(index);
  229.     };
  230.       
  231. /**
  232.  * SNMP PROTOCOL
  233.  * Transform this PDU to a sequence of characters, according to
  234.  * rules of the SNMP protocol.
  235.  */
  236.  
  237.     // Structure of PDU packet as I know it
  238.     // 
  239.     // Sequence {
  240.     //     Integer version = 0
  241.     //     String CommmunityName
  242.     //     PDU Type | SEQUENCE {
  243.     //         Integer request-id
  244.     //         Integer error-status
  245.     //         Integer error-index
  246.     //         SEQUENCE {
  247.     //             SEQUENCE {
  248.     //                 ObjectID name;
  249.     //                 Objecttype value;
  250.     //             }
  251.     //             ....
  252.     //         }
  253.     //     }
  254.     // }
  255.     
  256.     public ByteBuffer BERSerialize() {
  257.     _requestId++;
  258.     
  259.     ByteBuffer top = new ByteBuffer();
  260.     TypeInt vers = new TypeInt(thisVersion);
  261.     top.append(vers.BERSerialize());
  262.     TypeString comm = new TypeString(_communityName);
  263.     top.append(comm.BERSerialize());
  264.  
  265.     ByteBuffer packet = new ByteBuffer();
  266.     
  267.     // Request number
  268.     TypeInt req = new TypeInt(_requestId);
  269.     packet.append(req.BERSerialize());
  270.     // Error code
  271.     TypeInt err = new TypeInt(_errorStatus.value());
  272.     packet.append(err.BERSerialize());
  273.     // Error index
  274.     TypeInt errind = new TypeInt(_errorIndex);
  275.     packet.append(errind.BERSerialize());
  276.  
  277.     // List all variables.
  278.  
  279.     ByteBuffer varbuf = new ByteBuffer();
  280.     for(Enumeration iter = _varList.elements(); iter.hasMoreElements(); ) {
  281.         varbuf.append(((PDUVariable)iter.nextElement()).BERSerialize());
  282.     };
  283.     ByteBuffer newvarbuf = new ByteBuffer();
  284.     newvarbuf.append((byte) (ASN.SEQUENCE | ASN.CONSTRUCTOR));
  285.     newvarbuf.append(ASN.buildLength(varbuf.size()));
  286.     newvarbuf.append(varbuf);
  287.  
  288.     packet.append(newvarbuf);
  289.     ByteBuffer newpacket = new ByteBuffer();
  290.     newpacket.append((byte) (ASN.CONTEXT +  ASN.CONSTRUCTOR +  (byte) _command.value()));
  291.     newpacket.append(ASN.buildLength(packet.size()));
  292.     newpacket.append(packet);
  293.  
  294.     top.append(newpacket);
  295.  
  296.     ByteBuffer newtop = new ByteBuffer();
  297.     newtop.append((byte) (ASN.SEQUENCE | ASN.CONSTRUCTOR));
  298.     newtop.append(ASN.buildLength(top.size()));
  299.     newtop.append(top);
  300.  
  301.     return newtop;
  302.     };
  303.   
  304. /**
  305.  * String representation of a PDU
  306.  */
  307.  
  308.   public String toString() {
  309.       String result = "{ " + _version + ", " + 
  310.       _address + ", " +
  311.       _command + ", " +
  312.       _requestId + ", " +
  313.       _errorStatus + ", " +
  314.       _errorIndex + ", {";
  315.       for(Enumeration e = _varList.elements(); e.hasMoreElements(); )
  316.       result += e.nextElement().toString() + ", ";
  317.       result += "}}";
  318.       return result;
  319.   };
  320.  
  321. };
  322.